//
//  SpecailDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "SpecailDetailViewController.h"
#import "DetailTableViewCell.h"
#import "define.h"
#import "AsyncConnection.h"
#import "DetailTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SpecailWebViewController.h"

@interface SpecailDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, copy) NSString *flag;
@property (nonatomic, assign) CGFloat offHeigt;
@property (nonatomic, assign) CGFloat offWidth;

@end

@implementation SpecailDetailViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.offHeigt = [[UIScreen mainScreen]bounds].size.height / 667;
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.did = [self.dic objectForKey:@"id"];
    [self beginConnection];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    // Do any additional setup after loading the view.
}

- (void)back:(id)semder
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)beginConnection
{
    
    NSDictionary *dic = @{@"tuan_id":self.did,
                          @"flag":@"",
                          @"gv":@"6.3.0",
                          @"gs":@"640x1136",
                          @"gos":@"8.4",
                          @"gf":@"iphone",
                          @"gn":@"mxyc_ip",
                          @"gi":@"2CC19978-A0E5-49F2-9719-59DC5EDE6BBB",
                          @"gc":@"appstore",
                          @"access_token":@""
                          };
    
    [AsyncConnection asyncConnection:SPECAIL_DETAIL parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[dic objectForKey:@"data"]objectForKey:@"items"];
        [self createTableView];
    }path:@"main_specail_sale_detail"];
}

- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 69, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.backgroundColor = [UIColor colorWithRed:1 green:0 blue:1 alpha:0.2];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else
    {
        return self.array.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 300* _offHeigt;
    }else
    {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    UITableViewHeaderFooterView *myView = [tableView dequeueReusableCellWithIdentifier:@"heade"];
    if (section == 0) {
    if (myView == nil) {
        myView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"header"];
        
    }
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 200* _offHeigt)];
    image.backgroundColor = [UIColor redColor];
        [image sd_setImageWithURL:[NSURL URLWithString:[self.dic objectForKey:@"picUrl"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    [myView addSubview:image];
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(5 * _offWidth, 200* _offHeigt, self.view.frame.size.width - 10 *_offWidth, 80* _offHeigt)];
        title.numberOfLines = 0;
        
        title.textColor = [UIColor darkGrayColor];
        [myView addSubview:title];
        title.text = [[self.dic objectForKey:@"action"]objectForKey:@"description"];
        UIButton *date = [[UIButton alloc]initWithFrame:CGRectMake(0, 280* _offHeigt, self.view.frame.size.width / 2, 20* _offHeigt)];
        date.titleLabel.font = [UIFont systemFontOfSize:12];
        [myView addSubview:date];
//        [date setTitle:@"倒计时" forState:UIControlStateNormal];
        UIButton *people = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2, 280 * _offHeigt, self.view.frame.size.width / 2, 20 * _offHeigt)];
        people.titleLabel.font = [UIFont systemFontOfSize:12];
        [myView addSubview:people];
        [people setTitle:[[self.dic objectForKey:@"peopleCount"]stringByAppendingString:@"人正在抢购"] forState:UIControlStateNormal];
        [people setTitleColor:[UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.5] forState:UIControlStateNormal];
        
    
    }
    return myView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150* _offHeigt;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail"];
    if (cell == nil) {
        cell = [[DetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detail"];
    }
    NSDictionary *dic = [[self.array objectAtIndex:indexPath.row]objectForKey:@"component"];
    cell.title.text = [dic objectForKey:@"title"];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picUrl"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    [cell.people setTitle:[[dic objectForKey:@"peopleCount"]stringByAppendingString:@"想买"] forState:UIControlStateNormal];
    cell.price.text = [dic objectForKey:@"price"];
    
    cell.priceOrig.text = [dic objectForKey:@"priceOrig"];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picIcon"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    cell.discount.text = [dic objectForKey:@"discount"];
    cell.take.text = @"参加抢购>";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecailWebViewController *web = [[SpecailWebViewController alloc]init];
    NSDictionary *dic = [[[self.array objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"action"];
    web.name = self.discount;
    web.discount = [[[self.array objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"discount"];
    web.imageUrl = [self.dic objectForKey:@"picUrl"];
    web.desc = [[self.dic objectForKey:@"action"]objectForKey:@"description"];
    web.height = [[[[[self.array objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"action"]objectForKey:@"height"]floatValue];
    web.width = [[[[[self.array objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"action"]objectForKey:@"width"]floatValue];
    web.dic = [[self.array objectAtIndex:indexPath.row]objectForKey:@"component"];
    web.wid = [dic objectForKey:@"sourceId"];
    [self.navigationController pushViewController:web animated:YES];
    [web release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
